package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.ContractCompareVO;
import com.xbongbong.paas.pojo.vo.SearchDataInConditionVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.ContractCompareService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.cabinetfile.pojo.vo.CabinetFileListGetVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.contractCompare.pojo.dto.CheckCanLocalUploadDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareConditionDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareConsumptionDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareFileSelectDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareGetMoreSumDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareHistoryCompareDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareListDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareOrderDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareRefusedFileSelectDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareUpdateStaffDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareUrlDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractCompareUserDTO;
import com.xbongbong.pro.contractCompare.pojo.dto.ContractDataListDTO;
import com.xbongbong.pro.contractCompare.pojo.vo.ContractCompareConsumptionVO;
import com.xbongbong.pro.contractCompare.pojo.vo.ContractCompareMsgVO;
import com.xbongbong.pro.contractCompare.pojo.vo.ContractCompareOrderVO;
import com.xbongbong.pro.contractCompare.pojo.vo.ContractCompareUserListVO;
import com.xbongbong.pro.contractCompare.pojo.vo.ContractDataListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zhushuainan
 * @title: ContractCompareController
 * @projectName xbb-pro
 * @description: 合同比对controller
 * @date 2022/11/17 15:23
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/contractCompare")
public class ContractCompareController {

    @Autowired
    private ContractCompareService contractCompareService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @RequestMapping(value = "/initMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initSearchCustomer(@RequestBody @Valid MenuInitDTO menuInitDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuInitDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuInitDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                BaseVO baseVO = contractCompareService.initContractCompare(menuInitDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
        * @description: 坐席设置
        * @param contractCompareUpdateStaffDTO
        * @param br
        * @return java.lang.String
        * @author zhushuainan
        * @date 2022/11/23 10:52:52
        */
    @RequestMapping(value = "/set/staff", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setStaff(@RequestBody @Valid ContractCompareUpdateStaffDTO contractCompareUpdateStaffDTO, BindingResult br) {
        ContractCompareMsgVO contractCompareMsgVO;
        XbbResponse<ContractCompareMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                contractCompareMsgVO = contractCompareService.batchUpdateStaff(contractCompareUpdateStaffDTO);
                response = new XbbResponse<>(contractCompareMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/dep/userList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDepUserList(@RequestBody @Valid ContractCompareUserDTO contractCompareUserDTO, BindingResult br) {
        ContractCompareUserListVO contractCompareUserListVO;
        XbbResponse<ContractCompareUserListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                contractCompareUserListVO = contractCompareService.getDepUserList(contractCompareUserDTO);
                response = new XbbResponse<>(contractCompareUserListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid ContractCompareListDTO contractCompareListDTO, BindingResult br) {
        XbbResponse<ContractCompareVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(contractCompareListDTO.getSaasMark(), contractCompareListDTO.getBusinessType(), contractCompareListDTO.getMenuId(), contractCompareListDTO.getDistributorMark());
                ContractCompareVO contractCompareVO = contractCompareService.list(contractCompareListDTO);
                response = new XbbResponse<>(contractCompareVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/conditionList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String conditionList(@RequestBody @Valid ContractCompareConditionDTO contractCompareConditionDTO, BindingResult br) {
        XbbResponse<SearchDataInConditionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(contractCompareConditionDTO.getSaasMark(), contractCompareConditionDTO.getBusinessType(), contractCompareConditionDTO.getMenuId(), contractCompareConditionDTO.getDistributorMark());
                SearchDataInConditionVO searchDataInConditionVO = contractCompareService.conditionList(contractCompareConditionDTO);
                response = new XbbResponse<>(searchDataInConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/startCompare", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String startCompare(@RequestBody @Valid ContractCompareDTO contractCompareDTO, BindingResult br) {
        XbbResponse<JSONObject> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(contractCompareDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(contractCompareDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                JSONObject jsonObject = contractCompareService.startCompare(contractCompareDTO);
                response = new XbbResponse<>(jsonObject);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    @RequestMapping(value = "/fileSelect", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fileSelect(@RequestBody @Valid ContractCompareFileSelectDTO contractCompareFileSelectDTO, BindingResult br) {
        XbbResponse<CabinetFileListGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CabinetFileListGetVO cabinetFileListGetVO = contractCompareService.fileSelect(contractCompareFileSelectDTO);
                response = new XbbResponse<>(cabinetFileListGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/getRefusedfile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRefusedfile(@RequestBody @Valid ContractCompareRefusedFileSelectDTO contractCompareRefusedFileSelectDTO, BindingResult br) {
        XbbResponse<CabinetFileListGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CabinetFileListGetVO cabinetFileListGetVO = contractCompareService.getRefusedfile(contractCompareRefusedFileSelectDTO);
                response = new XbbResponse<>(cabinetFileListGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/checkCabinetFileCanSee", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkCabinetFileCanSee(@RequestBody @Valid CheckCanLocalUploadDTO checkCanLocalUploadDTO, BindingResult br) {
        XbbResponse<JSONObject> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                JSONObject jsonObject = contractCompareService.checkCanLocalUpload(checkCanLocalUploadDTO);
                response = new XbbResponse<>(jsonObject);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/tab/historyCompare", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String historyCompare(@RequestBody @Valid ContractCompareHistoryCompareDTO contractCompareHistoryCompareDTO, BindingResult br) {
        XbbResponse<ContractCompareVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(contractCompareHistoryCompareDTO.getSaasMark(), contractCompareHistoryCompareDTO.getBusinessType(), contractCompareHistoryCompareDTO.getMenuId(), contractCompareHistoryCompareDTO.getDistributorMark());
                ContractCompareVO contractCompareVO = contractCompareService.historyCompare(contractCompareHistoryCompareDTO);
                response = new XbbResponse<>(contractCompareVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @description: 用量获取
     * @param contractCompareConsumptionDTO
     * @param br
     * @return java.lang.String
     * @author zhushuainan
     */
    @RequestMapping(value = "/get/consumption", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConsumption(@RequestBody @Valid ContractCompareConsumptionDTO contractCompareConsumptionDTO, BindingResult br) {
        ContractCompareConsumptionVO contractCompareConsumptionVO;
        XbbResponse<ContractCompareConsumptionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                contractCompareConsumptionVO = contractCompareService.getConsumption(contractCompareConsumptionDTO);
                response = new XbbResponse<>(contractCompareConsumptionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
        * @description: TODO
        * @param contractCompareOrderDTO
        * @param br
        * @return java.lang.String
        * @author zhushuainan
        * @date 2022/11/25 09:23:01
        */
    @RequestMapping(value = "/get/moreInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMoreInfo(@RequestBody @Valid ContractCompareOrderDTO contractCompareOrderDTO, BindingResult br) {
        ContractCompareOrderVO contractCompareOrderVO;
        XbbResponse<ContractCompareOrderVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                contractCompareOrderVO = contractCompareService.getOrderInfo(contractCompareOrderDTO);
                response = new XbbResponse<>(contractCompareOrderVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get/moreCompareNum", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMoreCompareNum(@RequestBody @Valid ContractCompareGetMoreSumDTO contractCompareGetMoreSumDTO, BindingResult br) {
        BaseVO baseVO;
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                baseVO = contractCompareService.getMoreCompareNum(contractCompareGetMoreSumDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取合同列表
     * @param contractDataListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/get/contractDataList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getContractDataList(@RequestBody @Valid ContractDataListDTO contractDataListDTO, BindingResult br) {
        ContractDataListVO contractDataListVO;
        XbbResponse<ContractDataListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                contractDataListVO = contractCompareService.getContractDataList(contractDataListDTO);
                response = new XbbResponse<>(contractDataListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get/result", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getContractCompareResultUrl(@RequestBody @Valid ContractCompareUrlDTO contractCompareUrlDTO, BindingResult br) {
        JSONObject jsonObject;
        XbbResponse<JSONObject> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                jsonObject = contractCompareService.getContractCompareResultUrl(contractCompareUrlDTO);
                response = new XbbResponse<>(jsonObject);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
